/* Created on Jul 17, 2007 */
/**
 * 
 */
package com.xbang.core.struts.support;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.xbang.core.config.EntityNote;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.page.Page;
import com.xbang.core.utils.BeanUtils;

/**
 * @author dev.zhang
 */
public abstract class SearchActionSupport extends BaseActionSupport {

	private String pageCheckIds;
	
	private String actionURI;

	protected Map<String, Object> searchCriteria;

	protected SearchComponent searchComponent;

	protected List result;

	protected Page page = new Page();

	private String displayName;
	
	private String unPageCheckIds;

	private List<Map<String, String>> searchParameterKeys = new ArrayList<Map<String, String>>();

	// private List<>

	/**
	 * 提取SearchComponent信息
	 * 
	 * @param parameterKey
	 * @param wereSentenceKey
	 */
	public void extractSearchComponent(String parameterKey, String wereSentenceKey) {
		SearchComponent tmpSearchComponent = new SearchComponent();
		tmpSearchComponent.getParameters().putAll(searchComponent.getParameters());
		tmpSearchComponent.getWhereSentences().putAll(searchComponent.getWhereSentences());

		for (Iterator<Map.Entry<String, Object>> parameters = tmpSearchComponent.getParameters().entrySet().iterator(); parameters.hasNext();) {
			Map.Entry<String, Object> parameter = parameters.next();
			if (parameter.getKey().toLowerCase().equals(parameterKey.toLowerCase())) {
				searchComponent.getParameters().put(parameter.getKey(), parameter.getValue());
			}
		}

		for (Iterator<Map.Entry<String, String>> wereSentences = tmpSearchComponent.getWhereSentences().entrySet().iterator(); wereSentences.hasNext();) {
			Map.Entry<String, String> wereSentence = wereSentences.next();
			if (wereSentence.getKey().toLowerCase().equals(wereSentenceKey.toLowerCase())) {
				searchComponent.getWhereSentences().put(wereSentence.getKey(), wereSentence.getValue());
			}
		}
	}

	/**
	 * 根据Key拿到某个Value
	 * 
	 * @param key
	 * @return
	 */
	public Object getSearchParameterValue(String key) {
		for (Iterator<Map.Entry<String, Object>> parameters = searchComponent.getParameters().entrySet().iterator(); parameters.hasNext();) {
			Map.Entry<String, Object> parameter = parameters.next();
			if (parameter.getKey().toLowerCase().equals(key.toLowerCase())) {
				return parameter.getValue();
			}
		}
		return null;
	}

	/**
	 * 得到子信息
	 * 
	 * @param parameterKey
	 * @param wereSentenceKey
	 * @return
	 */
	public SearchComponent extractChildSearchComponent(String parameterKey, String wereSentenceKey) {
		SearchComponent tmpSearchComponent = new SearchComponent();

		for (Iterator<Map.Entry<String, Object>> parameters = searchComponent.getParameters().entrySet().iterator(); parameters.hasNext();) {
			Map.Entry<String, Object> parameter = parameters.next();
			if (parameter.getKey().toLowerCase().equals(parameterKey.toLowerCase())) {
				tmpSearchComponent.getParameters().put(parameter.getKey(), parameter.getValue());
			}
		}

		for (Iterator<Map.Entry<String, String>> wereSentences = searchComponent.getWhereSentences().entrySet().iterator(); wereSentences.hasNext();) {
			Map.Entry<String, String> wereSentence = wereSentences.next();
			if (wereSentence.getKey().toLowerCase().equals(wereSentenceKey.toLowerCase())) {
				tmpSearchComponent.getWhereSentences().put(wereSentence.getKey(), wereSentence.getValue());
			}
		}

		return tmpSearchComponent;
	}

	/**
	 * 
	 */
	public void resetSearchCriteria() {
		if (searchCriteria == null) {
			searchCriteria = new HashMap<String, Object>();
		}
		searchCriteria.put("searchComponent", searchComponent);
		resetCustomerSearchCriteria();
	}

	public abstract void resetCustomerSearchCriteria();

	/**
	 * @return the searchCriteria
	 */
	public Map<String, Object> getSearchCriteria() {
		return searchCriteria;
	}

	/**
	 * @return the searchComponent
	 */
	public SearchComponent getSearchComponent() {
		return searchComponent;
	}

	/**
	 * @return the result
	 */
	public List getResult() {
		return result;
	}

	/**
	 * @return the page
	 */
	public Page getPage() {
		return page;
	}

	/**
	 * @return the displayName
	 */
	public String getDisplayName() {

		if (searchParameterKeys == null || searchParameterKeys.isEmpty()) {
			Map<String, Object> parameters = searchComponent.getParameters();
			if (null != parameters && !parameters.isEmpty()) {
				// /generateParameterKey

			}
		}

		return displayName;
	}

	/**
	 * @param searchCriteria
	 *            the searchCriteria to set
	 */
	public void setSearchCriteria(Map<String, Object> searchCriteria) {
		this.searchCriteria = searchCriteria;
	}

	/**
	 * @param searchComponent
	 *            the searchComponent to set
	 */
	public void setSearchComponent(SearchComponent searchComponent) {
		this.searchComponent = searchComponent;
	}

	/**
	 * @param result
	 *            the result to set
	 */
	public void setResult(List result) {
		this.result = result;
	}

	/**
	 * @param page
	 *            the page to set
	 */
	public void setPage(Page page) {
		this.page = page;
	}

	/**
	 * @param displayName
	 *            the displayName to set
	 */
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	/**
	 * @return the searchParameterKeys
	 */
	public List<Map<String, String>> getSearchParameterKeys() {
		return searchParameterKeys;
	}

	/**
	 * @param searchParameterKeys
	 *            the searchParameterKeys to set
	 */
	public void setSearchParameterKeys(List<Map<String, String>> searchParameterKeys) {
		this.searchParameterKeys = searchParameterKeys;
	}

	public Class getClazz(Method method) {
		if (method.isAnnotationPresent(EntityNote.class)) {
			EntityNote entityNote = method.getAnnotation(EntityNote.class);
			return entityNote.entityClass();
		}
		return null;
	}

	public Class getClazz(String methodName) {
		Method method = BeanUtils.getBeanMethod(this.getClass(), methodName);
		if (method != null) {
			return getClazz(method);
		} else {
			return null;
		}
	}

	/**
	 * @return the pageCheckIds
	 */
	public String getPageCheckIds() {
		return pageCheckIds;
	}

	/**
	 * @param pageCheckIds
	 *            the pageCheckIds to set
	 */
	public void setPageCheckIds(String pageCheckIds) {
		if (pageCheckIds != null && !pageCheckIds.isEmpty()){
			String[] ids =pageCheckIds.split(",");
			String newIds = "";
			for (int index = 0 ; index < ids.length ; index++){
				
				if (!isExists(newIds.split(","),ids[index])){
					if (unPageCheckIds != null){
						if (isExists(unPageCheckIds.split(","), ids[index])){
							continue;
						}
					}
					if (newIds.length() == 0){
						newIds = ids[index];
					} else {
						newIds = newIds + "," + ids[index];
					}
				}
			}
			
			System.out.println(unPageCheckIds);
			
			pageCheckIds = newIds;
		}
		if (this.actionURI != null){
			page.setActionURI(actionURI);
		}
		this.pageCheckIds = pageCheckIds;
		if (page == null) {
			page = new Page();
		}
		page.setSelectCheckIds(pageCheckIds);
		// /lisen url change?so change values.
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		
		if (page.getActionURI() != null && !page.getActionURI().equals("url") && !page.getActionURI().equals(request.getRequestURI().toString())) {
			page.setSelectCheckIds("");
			page.setActionURI(request.getRequestURI().toString());
		}else{
			page.setActionURI(request.getRequestURI().toString());
		}
		
		
		
		///unPageCheckIds
	}
	
	private boolean isExists(String[] ids , String id){
		for (String vid:ids){
			if (!StringUtils.isNumeric(id)){
				return true;
			}
			if (vid.equals(id)){
				return true;
			}
		}
		return false;
	}

	/**
	 * @return the actionURI
	 */
	public String getActionURI() {
		return actionURI;
	}

	/**
	 * @param actionURI the actionURI to set
	 */
	public void setActionURI(String actionURI) {
		this.actionURI = actionURI;
		if (this.actionURI != null){
			page.setActionURI(actionURI);
		}
	}

	/**
	 * @return the unPageCheckIds
	 */
	public String getUnPageCheckIds() {
		return unPageCheckIds;
	}

	/**
	 * @param unPageCheckIds the unPageCheckIds to set
	 */
	public void setUnPageCheckIds(String unPageCheckIds) {
		this.unPageCheckIds = unPageCheckIds;
	}

	
	
	
}
